package hdfs;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.Progressable;
import org.junit.After;
import org.junit.Test;

public class FileSystemFullTest {
	
	protected static Log log = LogFactory.getLog(FileSystemFullTest.class);
	protected FileSystem fs;
	
	public FileSystemFullTest() throws IOException{
		
		fs = getFileSystem();
	}

	public FileSystem getFileSystem() throws IOException{
		
		Configuration conf = new Configuration();
		conf.set("mapred.job.tracker", "master:9001");
		conf.set("fs.default.name", "hdfs://master:9000");
		log.info("配置文件：" + conf);
		 
		FileSystem fs = FileSystem.get(conf);
		log.info("文件系统：" + fs);
		
		return fs;
	}
	
	@After
	public void testAfter() throws IOException{
		
		fs.close();
	}
	
	public Path[] list(String directory) throws IOException{
		
		FileStatus[] files = fs.listStatus(new Path(directory));
		Path[] paths = new Path[files.length];
		for(int i = 0; i < files.length; i++){
			
			paths[i] = files[i].getPath();
			log.info("路径：" + paths[i].toString());
		}
		return paths;
	}
	
	@Test
	public void testList() throws IOException{

		list("/");
	}
	
	public byte[] readAsByte(String file) throws IOException{
		
		Path path = new Path(file);
		if(!fs.exists(path)){
			
			log.info("不存在路径：‘" + file + "’的文件");
			return null;
		}
		
		FSDataInputStream in = fs.open(path);
		byte[] bytes = new byte[in.available()];
		in.readFully(bytes);
		return bytes;
	}
	
	@Test
	public void testReadAsByte() throws IOException{
		
		System.err.println(readAsByte("/user/hadoop/in/file1"));
	}
	
	public String readAsString(String file) throws IOException{
		
		return new String(readAsByte(file));
	}
	
	@Test
	public void testReadAsString() throws IOException{
		
		System.err.println(readAsString("/user/hadoop/in/file1"));
	}
	
	public void mkdir(String directory) throws IOException{
		
		fs.mkdirs(new Path(directory)); //一次性级联生成
	}
	
	@Test
	public void testMkdir() throws IOException{
		
//		mkdir("/test/home/tuyou");
		mkdir("/test/home/tuyou/tuyou2");
	}
	
	public void createFile(String filePath) throws IOException{
		
//		fs.createNewFile(new Path(filePath));
		fs.create(new Path(filePath), new Progressable() {
			
			@Override
			public void progress() {

				log.info("正在上传中。。。");
			}
		});
	}
	
	@Test
	public void testCreateFile() throws IOException{
		
		createFile("/test/home/tuyou/testFile1");
	}
	
	public void delete(String path) throws IOException{

//		fs.delete(new Path(path));
//		fs.delete(new Path(path), true);
		fs.deleteOnExit(new Path(path));
	}
	
	@Test
	public void testDelete() throws IOException{
		
		delete("/test/home/tuyou");
	}
	
	public void upload(String src, String dst) throws IOException{
		
		fs.copyFromLocalFile(new Path(src), new Path(dst));
	}
	
	@Test
	public void testUpload() throws IOException{
		
		upload("/Users/apple/log.log", "/test/home/tuyou/log.log");
	}
	
	public void download(String src, String dst) throws IOException{
		
		byte[] data = readAsByte(src);
		File file = new File(dst);
		
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(file);
			out.write(data);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
			if(out != null){
				
				out.close();
			}
		}
	}
	
	@Test
	public void testDownload() throws IOException{
		
		download("/test/home/tuyou/log.log", "/Users/apple/log2.log");
	}
	
	public void append(String path, String content) throws IOException{
		
		FSDataOutputStream output = null;
		try {
			output = fs.append(new Path(path));
			output.writeUTF(content);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
			if(output != null){
				
				output.close();
			}
		}
	}
	
	@Test
	public void testAppend() throws IOException{
		
		append("/test/home/tuyou/testFile1", "1");
		append("/test/home/tuyou/testFile1", "2");
		append("/test/home/tuyou/testFile1", "\r\n3");
	}
}
